<template>
  <el-dialog
    width="30%"
    title="选择业务系统"
    :visible.sync="params.visible"
  >
    <compTree
      :tree-show-checkbox="true"
      :tree-data="treeData"
      :highlight-current="true"
      :show-search="false"
      :comp-tree-height="400"
      :tree-default-checked-keys="treeDefaultCheckedKeys"
      @treeNodeCheck="treeNodeCheck"
    />
    <span
      slot="footer"
      class="dialog-footer"
    >
      <dialog-btn />

    </span>
  </el-dialog>
</template>
<script>
import { getComputerBusinessList } from '@/api/core'
import { SQLTable } from '@/../public/js/paramarr'
import Global from '@/../public/js/global'

export default {
  name: 'SelectSystem',
  props: {
    // eslint-disable-next-line vue/require-default-prop
    params: {
      type: Object,
      default() {
        return {
          visible: false,
          name: '' // 选择数据
        }
      }
    }
  },
  data() {
    return {
      treeData: [],
      treeDefaultCheckedKeys: [],
      resule: ''
    }
  },
  created() {
    getComputerBusinessList().then(vPar => {
      this.lpfInitWindow(vPar)
    })
  },
  methods: {
    treeNodeCheck(a, b) {
      this.resule = this.getRsturn(b.checkedNodes)
    },
    getRsturn(nodeCheck) {
      var arrID = []
      var arrName = []
      for (var i = 0; i < nodeCheck.length; i++) {
        var node = nodeCheck[i]
        arrID.push(node.id)
        arrName.push(node.text)
      }
      this.arrID = arrID
      var obj = {}
      obj.arrID = arrID
      obj.arrName = arrName
      return obj
    },
    lpfInitWindow(vPar) {
      var name = []
      if (this.params.name) {
        name = this.params.name.split(',')
      }
      var res = []
      var nodes = []
      // 分组
      var table = new SQLTable(vPar.getTable('k_table'))
      table.first()
      while (!table.eof()) {
        var node = {}
        // data
        node['id'] = Global.getGuid()
        node['text'] = table.getValueByName('Business')
        node['icon'] = 'yingyong2'
        if (name.indexOf(node['text']) > -1) {
          this.treeDefaultCheckedKeys.push(node['id'])
          res.push(node)
        }
        // add
        nodes.push(node)
        // next
        table.next()
      }
      this.resule = this.getRsturn(res)
      this.treeData = nodes
    },
    confirm() {
      // 如果当前传入回调则使用回调
      if (this.params.confirm !== undefined) {
        this.params.confirm(this.resule)
      } else {
        this.$emit('confirm', this.resule)
      }
    }
  }
}
</script>
